package yunwo.cn.yzstation.view.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import yunwo.cn.yzstation.R;
import yunwo.cn.yzstation.adapter.pageadapter;
import yunwo.cn.yzstation.base.MyBaseActivity;
import yunwo.cn.yzstation.bean.OrderBean.StaTrackingFee;
import yunwo.cn.yzstation.bean.OrderBean.StaTrackingInfo;
import yunwo.cn.yzstation.bean.OrderBean.StaTrackingInfoDetail;
import yunwo.cn.yzstation.bean.PackProductBean;
import yunwo.cn.yzstation.config.GoodsSP;
import yunwo.cn.yzstation.api.IorderActivityView;
import yunwo.cn.yzstation.presenter.OrderActivityPersenter;
import yunwo.cn.yzstation.view.fregment.openorder.CostFragment;
import yunwo.cn.yzstation.view.fregment.openorder.OrderFragment;

/**
 * Created by lao on 2017/9/5.
 */

public class OrderActivity extends MyBaseActivity implements IorderActivityView {
    public ViewPager mViewPager;
    private TabLayout tabLayout;
    public OrderFragment mOrderFragment;
    public CostFragment mCostFragment;
    private OrderActivityPersenter mOrderActivityPersenter;
    private StaTrackingInfo mStaTrackingInfo;
    private StaTrackingInfoDetail mStaTrackingInfoDetail;
    private StaTrackingFee mStaTrackingFee;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences.Editor edit = GoodsSP.getInstance().getEdit();
        edit.clear().commit();
        Log.d("jjjjjjjjjj", GoodsSP.getInstance().getGoodsShare().getString("trackingNumber", ""));
        initView();
        initEvent();
        mOrderActivityPersenter = new OrderActivityPersenter(this, this);
        // 获取运单号
        mOrderActivityPersenter.getWayBill();
        //获取默认品名包装
        mOrderActivityPersenter.getPackProduct();
        mStaTrackingInfo = new StaTrackingInfo();
        mStaTrackingInfoDetail = new StaTrackingInfoDetail();
        mStaTrackingFee = new StaTrackingFee();

    }

    public StaTrackingInfo getOrderInfoBean() {
        return mStaTrackingInfo;
    }

    public StaTrackingFee getStaTrackingFee() {
        return mStaTrackingFee;
    }

    public StaTrackingInfoDetail getStaTrackingInfoDetail() {
        return mStaTrackingInfoDetail;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    private void initEvent() {
        /*FragmentTransaction ft=fm.beginTransaction();
OneFragment oneFragment=new OneFragment();
ft.replace(R.id.frame,oneFragment,"f1");
Bundle bundle=new Bundle();
bundle.putString("dad","kkk");——这里的kkk就是要传的值
oneFragment.setArguments(bundle);
ft.commit();*/
        List<Fragment> list = new ArrayList<>();
        mOrderFragment = new OrderFragment();
        mCostFragment = new CostFragment();

        list.add(mOrderFragment);
        list.add(mCostFragment);
        pageadapter pageadapter = new pageadapter(getSupportFragmentManager(), list);
        mViewPager.setAdapter(pageadapter);
        tabLayout.setupWithViewPager(mViewPager);
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                setIndicator(tabLayout, 40, 40);
            }
        });

    }

    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        tabLayout = (TabLayout) findViewById(R.id.tablayout);

    }

    @Override
    protected String settiltile() {
        return "开单";
    }

    @Override
    protected int getLayoutRsid() {
        return R.layout.activity_order;
    }



    public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }


    @Override
    public void getPackProductSuccess(PackProductBean.DataBean dataBean) {
        if (mOrderFragment.mPackName != null && mOrderFragment.mProductName != null) {
            mOrderFragment.mProductName.setText(dataBean.getProductName());
            mOrderFragment.mPackName.setText(dataBean.getPackName());
        }
    }
}
